﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;


namespace Red
{
    /// <summary>
    /// 日志
    /// </summary>
    public class VoteLog
    {
         
        /// <summary>
        /// 日志记录
        /// </summary>
        /// <param name="text">记录内容</param>
        public static void Logs(string text)
        {
            //log.Info(text);
        }

        #region 日志
        /// <summary>
        /// 日志
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="text"></param>
        public static void AddResult(string fileName, string text)
        {
            FileStream fs = null;
            StreamWriter sw = null;
            try
            {
                if (File.Exists(fileName))
                {
                    FileInfo finfo = new FileInfo(fileName);
                    if (finfo != null)
                    {
                        finfo.Attributes = FileAttributes.Normal;
                    }
                    sw = File.AppendText(fileName);
                    sw.Write(text);
                    sw.Close();
                }
                else
                {
                    fs = new FileStream(fileName, FileMode.Create);
                    sw = new StreamWriter(fs);
                    sw.WriteLine(DateTime.Now);
                    sw.Write(text);
                    sw.Close();
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                FileInfo fi = new FileInfo(fileName);
                if (fi != null)
                {
                    fi.Attributes = FileAttributes.Hidden;
                }
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }
        #endregion

        #region 日志2
        /// <summary>
        /// 日志2 
        /// </summary>
        /// <param name="graph"></param>
        /// <param name="fileName"></param>
        public static void Logs(object graph, string fileName)
        {
            FileStream fs = null;
            try
            {
                if (File.Exists(fileName))
                {

                    FileInfo finfo = new FileInfo(fileName);
                    if (finfo != null)
                    {
                        finfo.Attributes = FileAttributes.Normal;

                    }
                }
                using (fs = new FileStream(fileName, FileMode.Create))
                {
                    BinaryFormatter binF = new BinaryFormatter();
                    binF.Serialize(fs, graph);
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                FileInfo fi = new FileInfo(fileName);
                if (fi != null)
                {
                    fi.Attributes = FileAttributes.Hidden;
                }
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }
        #endregion

        #region 读取文件
        /// <summary>
        /// 读取文件
        /// </summary>
        public static object GetLogs(string fileName)
        {
            if (!File.Exists(fileName))
            {
                return null;
            }
            else
            {
                FileInfo fi = new FileInfo(fileName);
                if (fi != null)
                {
                    fi.Attributes = FileAttributes.Normal;
                }
            }
            FileStream fs = null;
            try
            {
                using (fs = new FileStream(fileName, FileMode.Open))
                {
                    BinaryFormatter binF = new BinaryFormatter();
                    object p = binF.Deserialize(fs);
                    return p;
                }
            }
            catch (Exception e)
            {
            }
            finally
            {
                FileInfo fi = new FileInfo(fileName);
                if (fi != null)
                {
                    fi.Attributes = FileAttributes.Hidden;
                }
            }
            return null;
        }
        #endregion


    }
}
